#coding:utf-8

import wx
import cPickle as pickle
import wx.grid
import constants as CS

class SandBoxPanel(wx.Panel):
	
	def __init__(self,parent,size=(300,300)):
		wx.Panel.__init__(self,parent,-1,size=size)
		
		self.Bind(wx.EVT_MOTION,self.OnMotion)		
		self.Bind(wx.EVT_LEFT_UP ,self.OnLeftUp)		
		self.Bind(wx.EVT_LEFT_DOWN ,self.OnLeftDown)	
		
	def OnMotion(self,evt):
		for c in self.GetChildren():
			c._absPos = evt.GetPosition()
			c.doAction(evt.GetPosition())				

	def OnLeftDown(self,evt):
		for c in self.GetChildren():
			c._absPos = evt.GetPosition()
			
	def OnLeftUp(self,evt):
		for c in self.GetChildren():			
			c.resetState(128)
			

class PanelSource(wx.Panel):
	def __init__(self,parent,obj_context,obj_str,size=(40,40)):
		wx.Panel.__init__(self,parent,-1,size=size,style=wx.SIMPLE_BORDER)
		
		data = pickle.dumps(obj_context)
		self.source = wx.CustomDataObject(obj_str)		
		self.source.SetData(data)			
		
		self.SetMinSize(size)
		
		self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
		
	
	def OnLeftDown(self,evt):
		dropSource = wx.DropSource(self)
		dropSource.SetData(self.source)
		
		result = dropSource.DoDragDrop(wx.Drag_AllowMove)
		if result == wx.DragMove:
			print "FIM DROP"


class PanelTarget(wx.DropTarget):
	def __init__(self,window,custom_obj_str):
		wx.DropTarget.__init__(self)
		
		self.window = window
		self.data = wx.CustomDataObject(custom_obj_str)
		self.SetDataObject(self.data)	
		
	def OnData(self,x,y,res):
		self.GetData()
		
		drop_object_str = self.data.GetData()		
		drop_object = pickle.loads(drop_object_str)
		
		drop_object.render( self.window,(x,y) )
		
		return res


class InspectTable(wx.grid.PyGridTableBase):
    def __init__(self,iso):
        wx.grid.PyGridTableBase.__init__(self)
        
        self.iso = iso
        self.propertyMap = {}       
        self.rowLabels = []
        self.colLabels = [CS.TXT_COL_VALUE]
        
        rowCount = 0
        for pc in iso:#PropertyCollection
        	for op in pc:#ObjectProperty           
	            self.propertyMap.update( {rowCount:op.name} )
	            self.rowLabels.append(op.label)
	            rowCount+=1
        
        self.even=wx.grid.GridCellAttr()
        self.even.SetBackgroundColour("light gray")
        self.even.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
        
        self.odd=wx.grid.GridCellAttr()
        self.odd.SetBackgroundColour("white")
        self.odd.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))                
    
    def GetNumberRows(self):
        return self.propertyMap.__len__()
    
    def GetNumberCols(self):
        return 1
    
    def IsEmptyCell(self, row, col):
        return False
   
    def GetValue(self, row, col):
        return "%s" % self.iso.getProperty(self.propertyMap[row]).value
    
    def SetValue(self, row, col, value):
        self.iso.setProperty( self.propertyMap[row],value )
    
    def GetAttr(self, row, col, kind):
        attr = [self.even, self.odd][row % 2]
        attr.IncRef()
        return attr    
    
    def GetColLabelValue(self, col):
        return self.colLabels[col]
    
    def GetRowLabelValue(self, row):
        return self.rowLabels[row]
    
    		